package com.ruoyi.tester.executor.expression;

import com.ruoyi.tester.executor.ExpressionContext;
import org.springframework.stereotype.Component;

/**
 * @author SongJun
 * @description 逻辑与
 * @date 2022/4/29 10:07
 */
@Component
public class ExpAnd implements ExpressionAnalysis {
    @Override
    public String getExpressionName() {
        return "and";
    }

    @Override
    public String getDescription() {
        return "and(x ...) 返回当前布尔类型上下文(若没有上下文则默认上下文为true)与x等所有布尔类型参数做逻辑与运算的结果";
    }

    @Override
    public Object analysis(ExpressionContext context, String... param) {
        try {
            Boolean ret = true;
            if(context.getContext() != null){
                ret = (Boolean) context.getContext();
            }
            if(param != null){
                for (int i=0; i<param.length; i++){
                    Boolean cur = Boolean.valueOf(param[i]);
                    ret = ret && cur;
                }
            }
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
